<?php
//设置编码格式
header("Content-type:text/html;charset=utf-8");
//开启session
session_start();
//配置数据库
try{
    $pdo = new PDO('mysql:dbname=book;port=3306;host=127.0.0.1','root','NNcsj20010708');
    $pdo->exec('SET NAMES utf8');
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}catch(PDOException $e){
    die('数据库连接失败:' . $e->getMessage());
}

//添加 数据
function addsql($table,$array){
    global $pdo;
    $key ='';
    $val ='';
    foreach ($array as $k=>$v){
        $key .= "`{$k}`,";
        $val .= "'{$v}',";
    }
    $key = substr($key,0,strlen($key)-1);
    $val = substr($val,0,strlen($val)-1);
    $str = "INSERT INTO {$table} ({$key})VALUES({$val})";

    //echo $str;exit;
    if ($pdo->exec($str)) {
        return true;
    } else {
        return false;
    }

}

//修改数据
function update($table,$array,$where){
    global $pdo;
    $str = "UPDATE $table SET ";
    foreach ($array as $k=>$v){
        $str.= "$k='{$v}',";
    }
    $str = substr($str,0,strlen($str)-1);
    $str = $str.$where;
    if ($pdo->exec($str)) {
        return true;
    } else {
        return false;
    }
}


//1查询单挑数据
//2查询多条数据
function select($sql,$type = 1){
    global $pdo;
    $obj = $pdo->query($sql);
    if($type == 1){
        return $obj->fetch();
    }else{
        return $obj->fetchAll();
    }
}

//删除数据
function del($table,$id){
    global $pdo;
    $sql = "DELETE FROM ".$table." WHERE id =" . $id;
    //echo $sql;exit;
    if ($pdo->exec($sql)) {
        return true;
    } else {
        return false;
    }
}

//文件上传
function img($file='images',$path='../uploads/'){
    if(empty($_FILES[$file])){
        error($_SERVER['HTTP_REFERER'],'请上传文件');
    }
    $upfile=$_FILES[$file];
    $tmp_name=$upfile["tmp_name"];
    $imgname = time().$_FILES[$file]["name"];
    move_uploaded_file($tmp_name,$path.$imgname);
    $image = '/uploads/'.$imgname;
    return $image;
}

//系统提示 页面
function success($url,$msg='系统提示',$type = 1,$waitSecond = 3){
    echo <<<TIAOZHUAN
                <!DOCTYPE html>
                <html lang="zh-CN">
                <head>
                <title>系统信息</title>
                <link rel="stylesheet" href="css/pintuer.css">
                <style type="text/css">
                *{ margin:0px; padding:0px;}
                .error-container{ background:#fff; border:1px solid #0ae;  text-align:center; width:450px; margin:250px auto; font-family:Microsoft Yahei; padding-bottom:30px; border-top-left-radius:5px; border-top-right-radius:5px;  }
                .error-container h1{ font-size:16px; padding:12px 0; background:#0ae; color:#fff;}
                .errorcon{ padding:35px 0; text-align:center; color:#0ae; font-size:18px;}
                .errorcon i{ display:block; margin:12px auto; font-size:30px; }
                .errorcon span{color:red;}
                h4{ font-size:14px; color:#666;}
                a{color:#0ae;}
                </style>
                </head>
                <body class="no-skin">
                <div class="error-container">
                    <h1>系统提示</h1>
                    <div class="errorcon">
                        <i class="icon-smile-o"></i>$msg
                        <span style="display:none;"><i class="icon-frown-o"></i>操作失败!</span>
                   </div>
                    <h4 class="smaller">页面自动 <a id="href" href="$url">跳转</a> 等待时间： <b id="wait">$waitSecond</b></h4>

                </div>

                <script type="text/javascript">
                (function(){
                var wait = document.getElementById('wait'),href = document.getElementById('href').href;
                var interval = setInterval(function(){
                    var time = --wait.innerHTML;
                    if(time <= 0) {
                        location.href = href;
                        clearInterval(interval);
                    };
                }, 1000);
                })();
                </script>
                </body>
                </html>
TIAOZHUAN;
    die;
}

function error($url,$msg='系统提示',$waitSecond = 3){
    echo <<<TIAOZHUAN
                <!DOCTYPE html>
                <html lang="zh-CN">
                <head>
                <title>系统信息</title>
                <link rel="stylesheet" href="css/pintuer.css">
                <style type="text/css">
                *{ margin:0px; padding:0px;}
                .error-container{ background:#fff; border:1px solid #0ae;  text-align:center; width:450px; margin:250px auto; font-family:Microsoft Yahei; padding-bottom:30px; border-top-left-radius:5px; border-top-right-radius:5px;  }
                .error-container h1{ font-size:16px; padding:12px 0; background:#0ae; color:#fff;}
                .errorcon{ padding:35px 0; text-align:center; color:#0ae; font-size:18px;}
                .errorcon i{ display:block; margin:12px auto; font-size:30px; }
                .errorcon span{color:red;}
                h4{ font-size:14px; color:#666;}
                a{color:#0ae;}
                </style>
                </head>
                <body class="no-skin">
                <div class="error-container">
                    <h1>系统提示</h1>
                    <div class="errorcon">
                        <i class="icon-frown-o"></i>$msg!
                   </div>
                    <h4 class="smaller">页面自动 <a id="href" href="$url">跳转</a> 等待时间： <b id="wait">$waitSecond</b></h4>
                </div>
                <script type="text/javascript">
                (function(){
                var wait = document.getElementById('wait'),href = document.getElementById('href').href;
                var interval = setInterval(function(){
                    var time = --wait.innerHTML;
                    if(time <= 0) {
                        location.href = href;
                        clearInterval(interval);
                    };
                }, 1000);
                })();
                </script>
                </body>
                </html>
TIAOZHUAN;
    die;
}